#ifndef APPLEREMOTE
#define APPLEREMOTE

#include <QThread>
#include <QList>
#include <QMap>
#include <IOKit/hid/IOHIDLib.h>

class AppleRemote: public QThread
{
public:
    enum Event {
        VolumePlus = 0,
        VolumeMinus,
        Menu,
        Play,
        Right,
        Left,
        RightHold,
        LeftHold,
        MenuHold,
        PlaySleep,
        ControlSwitched
    };

    AppleRemote(QObject* receiver, bool exclusive = true);
    ~AppleRemote();

    bool isListeningToRemote();
    void startListening();
    void stopListening();

protected:
    void run();

private:
    void initCookieMap();
    io_object_t findAppleRemoteDevice();
    bool initCookies();
    bool createDeviceInterface(io_object_t hidDevice);
    bool openDevice();
    static void queueCallbackFunction(void* target, IOReturn result, void* refcon, void* sender);
    void handleEventWithCookieString(const QString& cookieString, int sumOfValues);

private:
    QObject* receiver;
    bool openInExclusiveMode;
    IOHIDDeviceInterface** hidDeviceInterface;
    IOHIDQueueInterface** queue;
    QList<IOHIDElementCookie> cookies;
    QMap<QString, Event> cookieToButtonMapping;
    int remoteId;
};

#endif // APPLEREMOTE
